package com.mall.b2c.controller;

import com.mall.b2c.domain.goods.GoodsCategoryDomain;
import com.mall.b2c.domain.intergral.IntergralDomain;
import com.mall.b2c.domain.intergral.ShopClassDomain;
import com.mall.b2c.domain.member.MemberDomain;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.controller.BaseController;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.util.PasswordUtil;
import com.mall.common.util.ResponseUtils;
import com.mall.common.util.VerifyUtil;
import com.mall.common.web.Servlets;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import com.mall.b2c.service.IntergralShopClassService;
/**
 * Created by chen on 16/12/15.
 */
@Controller
@RequestMapping("/admin/b2c/intergral_shop_class")
public class IntergralShopClassController extends BaseController {

    @RequestMapping(value="/treeData", method=RequestMethod.POST)
    @ResponseBody
    public List<ShopClassDomain> treeData(){
        String hql = "from ShopClassDomain bean";
        Finder f = Finder.create(hql);
        List<ShopClassDomain> catList = f.findHQLList();
        ShopClassDomain root = new ShopClassDomain();
        return catList;
    }

    @Resource
    IntergralShopClassService intergralShopClassService;
    /**
     * 跳转到列表页面
     */
    @RequestMapping(value="/list", method= RequestMethod.GET)
    public String list(Model model){
        return "/admin/b2c/intergralShopClass/list";
    }


    @RequestMapping(value="/add", method= RequestMethod.GET)
    public String add(Model model){
        return "/admin/b2c/intergralShopClass/input";
    }

    /**
     * 获取列表信息
     */
    @RequestMapping(value="/queryData", method=RequestMethod.POST)
    @ResponseBody
    public Pager queryData(Pager pager, HttpServletRequest request){
        Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
        List<SearchFilter> filters = SearchFilter.parse(filterParams);
        pager = intergralShopClassService.findPage(pager, filters);
        return JsonDataGridHelper.createJSONData(pager);
    }

    @RequestMapping(value="/save", method=RequestMethod.POST)
    @ResponseBody
    public String save(ShopClassDomain shopclass, HttpServletResponse response){

        if(!intergralShopClassService.isUnique("name", null, shopclass.getName())){
            throw new NoticeException("该产品名已经存在!");
        }
        intergralShopClassService.persist(shopclass);
        return ResponseUtils.renderJSON(successMessage, response);
    }
    @RequestMapping(value="/update", method=RequestMethod.POST)
    @ResponseBody
    public String update(ShopClassDomain shopclass, HttpServletResponse response){

        if(!intergralShopClassService.isUnique("name", null, shopclass.getName())){
            throw new NoticeException("该产品名已经存在!");
        }


        intergralShopClassService.update(shopclass);
        return ResponseUtils.renderJSON(successMessage, response);
    }

    /**
     * 修改产品
     * @param id 会员id
     * @param model
     * @return
     */
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        ShopClassDomain shopclass = intergralShopClassService.find(id);
        if(shopclass == null){
            throw new NoticeException("产品类型不存在!");
        }
        model.addAttribute("shopclass", shopclass);
        return "/admin/b2c/intergralShopClass/input";
    }
}
